from flask import jsonify
from typing import Any, Dict, Optional, Union


def create_response(
    status_code: int = 200,
    data: Optional[Dict[str, Any]] = None,
    message: str = "",
    **kwargs
) -> tuple:
    """
    创建统一的API响应格式
    
    Args:
        status_code: HTTP状态码
        data: 响应数据
        message: 响应消息
        **kwargs: 额外的响应字段
    
    Returns:
        tuple: (jsonify_response, status_code)
    """
    response_data = {
        "status": status_code,
        "data": data or {},
        "message": message
    }
    
    # 添加额外的字段
    response_data.update(kwargs)
    
    return jsonify(response_data), status_code


def success_response(
    data: Optional[Dict[str, Any]] = None,
    message: str = "操作成功",
    status_code: int = 200,
    **kwargs
) -> tuple:
    """
    创建成功响应
    
    Args:
        data: 响应数据
        message: 成功消息
        status_code: HTTP状态码
        **kwargs: 额外的响应字段
    
    Returns:
        tuple: (jsonify_response, status_code)
    """
    return create_response(
        status_code=status_code,
        data=data,
        message=message,
        **kwargs
    )


def error_response(
    message: str,
    status_code: int = 400,
    data: Optional[Dict[str, Any]] = None,
    **kwargs
) -> tuple:
    """
    创建错误响应
    
    Args:
        message: 错误消息
        status_code: HTTP状态码
        data: 错误相关数据
        **kwargs: 额外的响应字段
    
    Returns:
        tuple: (jsonify_response, status_code)
    """
    return create_response(
        status_code=status_code,
        data=data,
        message=message,
        **kwargs
    )


def not_found_response(message: str = "资源未找到") -> tuple:
    """创建404响应"""
    return error_response(message=message, status_code=404)


def permission_denied_response(message: str = "权限不足") -> tuple:
    """创建403响应"""
    return error_response(message=message, status_code=403)


def server_error_response(message: str = "服务器内部错误") -> tuple:
    """创建500响应"""
    return error_response(message=message, status_code=500)


def bad_request_response(message: str = "请求参数错误") -> tuple:
    """创建400响应"""
    return error_response(message=message, status_code=400) 